package com.zyg.demo.control;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.listener.OnItemDragListener;
import com.zyg.demo.R;
import com.zyg.demo.entity.AnimItem;
import com.zyg.demo.entity.FolderBgHideItem;
import com.zyg.demo.entity.FolderBgShowItem;
import com.zyg.demo.listener.Drag2FolderListener;
import com.zyg.demo.listener.DragListener;
import com.zyg.demo.util.Constants;

/**
 * 用户绘制文件夹背景以及控制展示动画
 *
 * @author zhengyunguang
 * created at 2019-07-10 15:13
 */
public class DragControler extends RecyclerView.ItemDecoration implements DragListener, OnItemDragListener {


    /**
     * 文件夹画笔
     */
    private Paint folderFillPaint;
    private Paint folderStrokePaint;
    /**
     * 动画控制
     */
    private FolderBgShowItem folderShowItem;
    private FolderBgHideItem folderHideItem;

    private RecyclerView.ViewHolder targetFolderHolder;

    private RecyclerView.ViewHolder selectHolder;

    /**
     * 监听释放动作
     */
    private Drag2FolderListener onDrag2FolderListener;

    public DragControler(int fillColor, int stokeColor) {
        //文件夹背景
        folderFillPaint = new Paint();
        folderFillPaint.setAntiAlias(true);
        folderFillPaint.setColor(fillColor);
        folderFillPaint.setStyle(Paint.Style.FILL);
        //文件夹边缘
        folderStrokePaint = new Paint();
        folderStrokePaint.setAntiAlias(true);
        folderStrokePaint.setColor(stokeColor);
        folderStrokePaint.setStyle(Paint.Style.STROKE);
    }

    public void setOnDrag2FolderListener(Drag2FolderListener onDrag2FolderListener) {
        this.onDrag2FolderListener = onDrag2FolderListener;
    }

    @Override
    public void onChooseNearBy(RecyclerView.ViewHolder viewHolder) {
        targetFolderHolder = viewHolder;
        if (viewHolder != null) {
            int pos = viewHolder.getLayoutPosition();
            if (folderShowItem != null) {
                int posLast = folderShowItem.pos;
                if (posLast != pos) {
                    folderHideItem = new FolderBgHideItem(folderShowItem.viewHolder, posLast, folderShowItem.scale);
                    folderHideItem.hide();
                    folderShowItem = new FolderBgShowItem(viewHolder, pos);
                    folderShowItem.show();
                }
            } else {
                folderShowItem = new FolderBgShowItem(viewHolder, pos);
                folderShowItem.show();
            }
        } else {
            if (folderShowItem != null) {
                folderHideItem = new FolderBgHideItem(folderShowItem.viewHolder, folderShowItem.pos, folderShowItem.scale);
                folderHideItem.hide();
                folderShowItem = null;
            }
        }
    }

    @Override
    public boolean canSearchFolder(RecyclerView.ViewHolder viewHolder) {
        if (viewHolder == null || viewHolder.getItemViewType() == Constants.TYPE_FOLDER) {
            return false;
        }
        return true;
    }

    @Override
    public void onDragRelease() {
        if (targetFolderHolder != null && selectHolder != null && onDrag2FolderListener != null) {
            if (selectHolder.getItemViewType() == Constants.TYPE_PLUGIN) {
                int pos = selectHolder.getLayoutPosition();
                if (targetFolderHolder.getItemViewType() == Constants.TYPE_PLUGIN) {
                    onDrag2FolderListener.onDrag2Folder(true, selectHolder, pos, targetFolderHolder, targetFolderHolder.getLayoutPosition());
                } else {
                    onDrag2FolderListener.onDrag2Folder(false, selectHolder, pos, targetFolderHolder, targetFolderHolder.getLayoutPosition());
                }
            }
        }
        selectHolder = null;
    }

    @Override
    public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos) {
        selectHolder = viewHolder;
        folderShowItem = null;
        folderHideItem = null;
        targetFolderHolder = null;
    }

    @Override
    public void onItemDragMoving(RecyclerView.ViewHolder source, int from, RecyclerView.ViewHolder target, int to) {
        folderShowItem = null;
        folderHideItem = null;
    }

    /**
     * 动画结束才走这里，刚释放是走onDragRelease
     *
     * @param viewHolder
     * @param pos
     */
    @Override
    public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {
        /**
         * 只能置空，viewholder刷新不会引起Decoration的刷新
         */
//        if (folderShowItem != null) {
//            folderHideItem = new HideItem(folderShowItem.viewHolder, folderShowItem.pos, folderShowItem.scale);
//            folderHideItem.hide();
//            folderShowItem = null;
//        }
        folderShowItem = null;
        folderHideItem = null;
    }

    @Override
    public void onDraw(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(canvas, parent, state);
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            if (folderShowItem != null && folderShowItem.pos == i) {
                drawItemScaleFolder(folderShowItem, parent, canvas);
            }
            if (folderHideItem != null && folderHideItem.pos == i) {
                drawItemScaleFolder(folderHideItem, parent, canvas);
            }
        }
    }

    @Override
    public void onDrawOver(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDrawOver(canvas, parent, state);
//        RectF rect = new RectF();
//        rect.set(100, 100, 300, 300);
//        canvas.drawRect(rect, folderFillPaint);
    }

    /**
     * 根据父布局和子布局计算出每个item要回执的background
     *
     * @param item
     * @param parent
     * @param canvas
     */
    private void drawItemScaleFolder(AnimItem item, @NonNull RecyclerView parent, @NonNull Canvas canvas) {
        View child = parent.getChildAt(item.pos);
        View imageView = child.findViewById(R.id.iv_plugin);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
        int leftOffset = child.getLeft() + params.leftMargin;
        int topOffset = child.getTop();
        int widthOffset = (int) (imageView.getWidth() * item.getScale() - imageView.getWidth());
        int heightOffset = (int) (imageView.getHeight() * item.getScale() - imageView.getHeight());
        int left = leftOffset + imageView.getLeft() - widthOffset / 2;
        int right = leftOffset + imageView.getRight() + widthOffset / 2;
        int top = topOffset + imageView.getTop() - heightOffset / 2;
        int bottom = topOffset + imageView.getBottom() + heightOffset / 2;
        RectF rect = new RectF();
        rect.set(left, top, right, bottom);
        canvas.drawRoundRect(rect, 30, 30, folderFillPaint);
        canvas.drawRoundRect(rect, 30, 30, folderStrokePaint);
    }

}
